//
//  SRDevice+Cate.m
//  SportRing
//
//  Created by Apple on 14/10/27.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRDevice+Cate.h"
#import "SRMe+Cate.h"
#import <CoreData+MagicalRecord.h>

@implementation SRDevice (Cate)

+ (SRDevice *)deivce
{
    __block SRDevice * device = nil;
    [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:^(NSManagedObjectContext *localContext)
     {
         device = [SRDevice MR_createEntity];
     }];
    return device;
}

- (void)configAttributesWithBlock:(void(^)(SRDevice * deviceSelf))block
{
    [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:^(NSManagedObjectContext *localContext)
     {
         block(self);
     }];
}

@end

@implementation SRDevice (Find)

+ (SRDevice *)findDeviceForMe:(SRMe *)me
{
    return me.device;
}

+ (SRDevice *)findDeviceCINEForMe:(SRMe *)me
{
    SRDevice * device = [SRDevice findDeviceForMe:me];
    if (device == nil) {
        device = [SRDevice deivce];
        [device configAttributesWithBlock:^(SRDevice *deviceSelf) {
            deviceSelf.me = me;
        }];
    }
    return device;
}

@end

@implementation SRDevice (Delete)

+ (void)deleteDevice:(SRDevice *)device
{
    [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:^(NSManagedObjectContext *localContext)
     {
         [device MR_deleteEntity];
     }];
}

@end